package com.splant.android.smart.LogLevel;

import android.content.Context;

import com.splant.android.smart.SystemDefinedLevel.FilesManager.FilesManager;
import com.splant.android.smart.SystemDefinedLevel.Utils.System_Util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.LinkedList;

/**
 * Created by Administrator on 2016/9/15.
 */
public abstract class BaseLog {

    protected final int LOG_ASSERT_MAXCOUNT = 500;
    protected final int LOG_DEBUG_MAXCOUNT = 500;
    protected final int LOG_ERROR_MAXCOUNT = 500;
    protected final int LOG_INFO_MAXCOUNT = 1000;
    protected final int LOG_VERBOSE_MAXCOUNT = 500;
    protected final int LOG_WARN_MAXCOUNT = 500;

    protected final LinkedList<MyLog> assertLogList = new LinkedList<>();
    protected final LinkedList<MyLog> debugLogList = new LinkedList<>();
    protected final LinkedList<MyLog> errorLogList = new LinkedList<>();
    protected final LinkedList<MyLog> infoLogList = new LinkedList<>();
    protected final LinkedList<MyLog> verboseLogList = new LinkedList<>();
    protected final LinkedList<MyLog> warnLogList = new LinkedList<>();

    private FilesManager mFilesManager;

    public BaseLog(Context context) {

        mFilesManager = new FilesManager(context);

    }

    protected JSONObject createJsonLog(MyLog myLog) {
        JSONObject logObj = null;
        try {
            logObj = new JSONObject();
            logObj.put("id", myLog.logId);
            logObj.put("date", myLog.logDate);
            logObj.put("tag", myLog.logTag);
            logObj.put("msg", myLog.logMsg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return logObj;
    }

    protected JSONArray getJsonArrayLogs(LinkedList<MyLog> myLogList) {
        JSONArray logJsonArray = new JSONArray();
        for (MyLog myLog : myLogList)
            logJsonArray.put(myLog);

        return logJsonArray;
    }

    protected void saveLog(String fileSrc, String logsContent) {
        mFilesManager.saveFile(fileSrc, logsContent, System_Util.CHARSETNAME_GBK);
    }

    protected MyLog getMyLog(int logId,String logTag,String logMsg)
    {
        MyLog myLog=new MyLog();
        myLog.logId=logId;
        myLog.logDate=new Date();
        myLog.logTag=logTag;
        myLog.logMsg=logMsg;

        return myLog;
    }
    protected void addLog(LinkedList<MyLog> myLogList,int maxCount,int logId,String logTag,String logMsg)
    {
        MyLog newLog=getMyLog(logId,logTag,logMsg);

        if (myLogList.size()>=maxCount)
             myLogList.removeLast();

        myLogList.addFirst(newLog);
    }
}
